package ro.gillardon.business.priceservice.impl.hotel;

import java.util.ArrayList;
import java.util.List;

import ro.gillardon.business.exception.BusinessException;
import ro.gillardon.commons.Conditions;
import ro.gillardon.commons.enums.City;
import ro.gillardon.commons.enums.Hotel;
import ro.gillardon.commons.hotel.HotelConditions;
import ro.gillardon.commons.hotel.HotelDetailsWrapper;
import ro.gillardon.persistence.DAOFactory;
import ro.gillardon.persistence.HotelDetailsDAO;
import ro.gillardon.persistence.exception.PersistenceException;

public class HotelService {

  public HotelService() {
    super();
  }

  private HotelConditions checkForCorrectType(final Conditions conditions)
    throws BusinessException {
    if (!(conditions instanceof HotelConditions)) {
      throw new BusinessException();
    }
    return (HotelConditions) conditions;
  }

  public HotelDetailsWrapper getOffers(final Conditions conditions) throws
    BusinessException {

    try {
      HotelConditions hotelConditions = checkForCorrectType(conditions);

      HotelDetailsDAO hotelDetailsDAO = DAOFactory.getDAOFactory().getHotelDetailsDAO();

      HotelDetailsWrapper hotelDetails = hotelDetailsDAO.getLatestHotelOffer(hotelConditions.getHotel());

      return hotelDetails;

    } catch (PersistenceException e) {
      throw new BusinessException(e.getMessage(), e);
    }
  }

  public List<String> getHotelsForLocation(final String city) throws
  BusinessException {
    try {
      HotelDetailsDAO hotelDetailsDAO = DAOFactory.getDAOFactory().getHotelDetailsDAO();
      List<String> results = new ArrayList<String>();
      List<Hotel> hotels = hotelDetailsDAO.getAllHotelsFromLocation(City.valueOf(city));

      for (Hotel h : hotels) {
        results.add(h.toString());
      }
      return results;

    } catch (PersistenceException e) {
      throw new BusinessException(e.getMessage(), e);
    }
  }

  public List<String> getAllLocations() throws BusinessException {
    try {
      HotelDetailsDAO hotelDetailsDAO = DAOFactory.getDAOFactory().getHotelDetailsDAO();
      List<String> results = new ArrayList<String>();

      List<City> cities = hotelDetailsDAO.getAllLocations();

      for (City c : cities) {
        results.add(c.toString());
      }

      return results;

    } catch (PersistenceException e) {
      throw new BusinessException(e.getMessage(), e);
    }
  }
}
